package com.ikkong.biz.controller;

import java.util.Date;
import java.util.List;

import org.beetl.sql.ext.jfinal.Trans;

import com.fj.hiwetoptools.PinyinUtil;
import com.ikkong.common.vo.User;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.AdminBaseController;
import com.ikkong.biz.model.Teacher;
import com.ikkong.biz.service.AsyncWeixinService;
import com.ikkong.biz.service.TeacherService;
import com.ikkong.biz.service.AsyncWeixinService.OPTYPE;
import com.ikkong.biz.service.impl.AsyncWeixinServiceImpl;
import com.ikkong.biz.service.impl.TeacherServiceImpl;
import com.jfinal.aop.Before;
import com.jfinal.upload.UploadFile;

/**
 * Generated by Blade.
 * 2016-11-16 10:38:28
 * tb_teacher表从10000开始自增，避免与tfw_dept表出现id冲突
 */
public class TeacherController extends AdminBaseController {
	private static String CODE = "teacher";
	private static String PERFIX = "tb_teacher";
	private static String LIST_SOURCE = "Teacher.list";
	private static String BASE_PATH = "/biz/teacher/";
	
	TeacherService service = new TeacherServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "teacher.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "teacher_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Teacher teacher = service.findById(id);
		setAttr("model", JsonKit.toJson(teacher));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "teacher_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Teacher teacher = service.findById(id);
		setAttr("model", JsonKit.toJson(teacher));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "teacher_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}
	
	public void coorganizer() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	@Before(Trans.class)
	public void save() {
		Teacher teacher = mapping(PERFIX, Teacher.class);
		Teacher dbTea = service.findFirstBy("mobile="+teacher.getMobile(), null);
		if(null !=dbTea){
			renderJson(error("手机号已经存在,请重新输入"));
			return;
		}
		teacher.setVersion(0);
		teacher.setFirst_spell(PinyinUtil.getPinyinFirst(teacher.getName().substring(0,1)));
        teacher.setCtime(new Date());
		teacher.setVersion(0);

		boolean temp = service.saveWithUser(teacher);

		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Teacher teacher = mapping(PERFIX, Teacher.class);
		Teacher dbTea = service.findFirstBy("mobile="+teacher.getMobile()+" and id!="+teacher.getId(), null);
		if(null !=dbTea){
			renderJson(error("手机号已经存在,请重新输入"));
			return;
		}
		Teacher db = service.findById(teacher.getId());
		
		if(!db.getMobile().equals(teacher.getMobile())){
			//手机号修改，需要修改对应的登录名
			User user = Blade.create(User.class).findFirstBy(" where account = '"+db.getMobile()+"'", null);
			db.setMobile(teacher.getMobile());
			user.setAccount(teacher.getMobile());
			Blade.create(User.class).update(user);			
		}
		
		db.setVersion(teacher.getVersion()+1);
		db.setFirst_spell(PinyinUtil.getPinyinFirst(teacher.getName().substring(0,1)));
		db.setUtime(new Date());
		db.setName(teacher.getName());
		
		db.setPosition(teacher.getPosition());
		db.setNotice_tag_id(teacher.getNotice_tag_id());
		boolean temp = service.updateEveryCol(db);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
        Db.init().delete("delete from tfw_user where teacher_id in (#{join(ids)})", Record.create().set("ids",ids.split(",")));
//        Blade.create(User.class).deleteBy(" teacher_id in (#{ids})", Record.create().set("ids",ids.split(",")));
		String[] array = ids.split(",");		
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}

    public void toImport(){
        setAttr("code", CODE);
        render(BASE_PATH + "teacher_import.html");
    }

    public void doImport(){
		List<UploadFile> flist = this.getFiles("/temp", 1024*1024*50);
		if(flist.size()>0){
			UploadFile uf=flist.get(0);
	        String result = service.doImport(uf.getFile());
			renderJson(success(result));
		}

    }

    /**
     * 老师归属部门
     */
    public void chooseDept() {
        String depts = getPara("ids");
        int id = this.getParaToInt("id",0);
        boolean temp = service.chooseDept(id,depts);
        if (temp) {
            renderJson(success(UPDATE_SUCCESS_MSG));
        } else {
            renderJson(error(UPDATE_FAIL_MSG));
        }
    }
}
